package com.chinairi.mall.service.impl;

import com.chinairi.common.annotation.DoPage;
import com.chinairi.common.utils.DateUtils;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.controller.viewobject.OrderInforGoodsVO;
import com.chinairi.mall.dao.CompShopMapper;
import com.chinairi.mall.dao.OrderGoodsMapper;
import com.chinairi.mall.dao.OrderInfoMapper;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.ArrivalDateUpdate;
import com.chinairi.mall.dao.ArrivalDateUpdateMapper;
import com.chinairi.mall.model.CompShop;
import com.chinairi.mall.model.OrderGoods;
import com.chinairi.mall.model.OrderInfo;
import com.chinairi.mall.service.IArrivalDateUpdateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinairi.mall.service.modelobject.ArrivalDateUpdateMO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 董志涵
 * @since 2020-10-21
 */
@Service
public class ArrivalDateUpdateServiceImpl extends ServiceImpl<ArrivalDateUpdateMapper, ArrivalDateUpdate> implements IArrivalDateUpdateService {
    @Autowired
    private ArrivalDateUpdateMapper arrivalDateUpdateMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderGoodsMapper orderGoodsMapper;
    @Autowired
    private CompShopMapper compShopMapper;

    /**
     * 新增修改到货日期
     * @param arrivalDateUpdate
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(@RequestBody ArrivalDateUpdate arrivalDateUpdate){
        String userId = UserSession.getUserId();
//        String userId = "9ae23a7a28374e4c9f620e64cfd04c0d";
        CompShop compShop = compShopMapper.getAllByType("0",userId);
        arrivalDateUpdate.setAduId(UUID.randomUUID().toString().replace("-", ""));
        arrivalDateUpdate.setCreaterId(userId);
        arrivalDateUpdate.setCreater(UserSession.getLoginName());
        OrderInfo orderInfo = orderInfoMapper.selectById(arrivalDateUpdate.getOrderId());
        arrivalDateUpdate.setReceiverId(orderInfo.getCreateUserId());
        arrivalDateUpdate.setReceiver(orderInfo.getCreateUser());
        arrivalDateUpdate.setShopId(compShop.getCsDbid());
        arrivalDateUpdate.setShopName(compShop.getCsShopName());
        arrivalDateUpdate.setUrl("purchaser/shoppingCart/orderState");
        arrivalDateUpdate.setCreaterTime(new Date());
        arrivalDateUpdateMapper.insert(arrivalDateUpdate);
    }

    /**
     * 修改 修改到货日期
     * @param arrivalDateUpdate
     */
    @Override
    public void update(@RequestBody ArrivalDateUpdate arrivalDateUpdate){
        arrivalDateUpdateMapper.updateById(arrivalDateUpdate);
    }

    /**
     * 修改 处理标志位
     * @param arrivalDateUpdate
     */
    @Override
    public void updateFlag(ArrivalDateUpdate arrivalDateUpdate) throws ParseException {
        ArrivalDateUpdate arrivalDateUpdate1 = arrivalDateUpdateMapper.selectById(arrivalDateUpdate.getAduId());
        Date now = new Date();
        if(!compare(arrivalDateUpdate1.getDeadline(),DateUtils.getDateString(now,"yyyy-MM-dd"))){
            if(arrivalDateUpdate.getUpdateFlag().equals("1")){
                String ogId = arrivalDateUpdate1.getGoodsId();
                OrderGoods orderGoods = orderGoodsMapper.selectById(ogId);
                orderGoods.setOgDeliveryData(arrivalDateUpdate1.getNewDate());
                orderGoodsMapper.updateById(orderGoods);
            }
            arrivalDateUpdateMapper.updateFlag(arrivalDateUpdate);
        }
    }

    /**
     * 比较两个时间大小
     *
     * @param a
     * @param time2
     * @return
     * @throws ParseException
     */
    public boolean compare(Date a, String time2) throws ParseException {
        //如果想比较日期则写成"yyyy-MM-dd"就可以了
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //将字符串形式的时间转化为Date类型的时间
//        Date a=sdf.parse(time1);
        Date b = sdf.parse(time2);
        //Date类的一个方法，如果a早于b返回true，否则返回false
        if (a.before(b)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     *买家用户获取修改到货日期的待办
     */
    @Override
    @DoPage
    public List<ArrivalDateUpdateMO> getByReceiverId(){
        String receiverId = UserSession.getUserId();
        return arrivalDateUpdateMapper.getByReceiverId(receiverId);
    }

    /**
     * 自动更新到期的待办的状态
     */
    @Override
    public void updateFlagAuto(){
        Date now = new Date();
        arrivalDateUpdateMapper.updateFlagAuto(now);
    }

    /**
     * 买家调整到货日期后修改
     */
    @Override
    public void updateDate(String aduId){
        ArrivalDateUpdate arrivalDateUpdate = arrivalDateUpdateMapper.selectById(aduId);
        if(arrivalDateUpdate == null){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "没有卖家调整到货日期的请求");
        }else{
            Date newDate = arrivalDateUpdate.getNewDate();
            String orderId = arrivalDateUpdate.getOrderId();
            String goodsId = arrivalDateUpdate.getGoodsId();
            arrivalDateUpdateMapper.updateDate(orderId,newDate,goodsId);
        }
    }

    /**
     * 新增修改到货日期
     * @param arrivalDateUpdate
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addRemind(@RequestBody ArrivalDateUpdate arrivalDateUpdate){
        String userId = UserSession.getUserId();
//        String userId = "9ae23a7a28374e4c9f620e64cfd04c0d";
//        CompShop compShop = compShopMapper.getAllByType("0",userId);
        arrivalDateUpdate.setAduId(UUID.randomUUID().toString().replace("-", ""));
        arrivalDateUpdate.setCreaterId(userId);
        arrivalDateUpdate.setCreater(UserSession.getLoginName());
        arrivalDateUpdate.setUpdateFlag("10");
//        OrderInfo orderInfo = orderInfoMapper.selectById(arrivalDateUpdate.getOrderId());
//        arrivalDateUpdate.setReceiverId(orderInfo.getCreateUserId());
//        arrivalDateUpdate.setReceiver(orderInfo.getCreateUser());
//        arrivalDateUpdate.setShopId(compShop.getCsDbid());
//        arrivalDateUpdate.setShopName(compShop.getCsShopName());
//        arrivalDateUpdate.setUrl("purchaser/shoppingCart/orderState");
        arrivalDateUpdate.setCreaterTime(new Date());
        Date newDate = arrivalDateUpdate.getNewDate();
        String orderId = arrivalDateUpdate.getOrderId();
        String goodsId = arrivalDateUpdate.getGoodsId();
        arrivalDateUpdateMapper.updateDate(orderId,newDate,goodsId);
        arrivalDateUpdateMapper.insert(arrivalDateUpdate);
    }

    /**
     *卖家用户获取修改到货日期的提醒
     */
    @Override
    @DoPage
    public List<ArrivalDateUpdateMO> getRemindByShopId(){
        String receiverId = UserSession.getUserId();
        CompShop compShop = compShopMapper.getAllByType("0",receiverId);
        return arrivalDateUpdateMapper.getRemindByShopId(compShop.getCsDbid());
    }
}
